import 'package:flutter/material.dart';

class PageViewExample extends StatefulWidget {
  const PageViewExample({super.key});

  @override
  _PageViewExampleState createState() => _PageViewExampleState();
}

class _PageViewExampleState extends State<PageViewExample> with AutomaticKeepAliveClientMixin{
  final PageController _pageController = PageController(initialPage: 0);
  final List<Widget> _pages = [
    const Center(child: Text('Page 1', style: TextStyle(fontSize: 24))),
    const Center(child: Text('Page 2', style: TextStyle(fontSize: 24))),
    const Center(child: Text('Page 3', style: TextStyle(fontSize: 24))),
  ];

  void _printCurrentPageName(int pageIndex) {
    switch (pageIndex) {
      case 0:
        print('Current Page: Page 1');
        break;
      case 1:
        print('Current Page: Page 2');
        break;
      case 2:
        print('Current Page: Page 3');
        break;
      default:
        print('Current Page: Unknown Page');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('PageView Example')),
      body: PageView(
        controller: _pageController,
        onPageChanged: _printCurrentPageName,
        children: _pages,
      ),
      bottomNavigationBar: BottomAppBar(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            IconButton(
              icon: const Icon(Icons.arrow_back),
              onPressed: () {
                _pageController.previousPage(
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeInOut,
                );
              },
            ),
            IconButton(
              icon: const Icon(Icons.arrow_forward),
              onPressed: () {
                _pageController.nextPage(
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeInOut,
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;   // 是否需要缓存
}